/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.deprecated.nimadaili;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;

import java.time.Duration;
import java.util.List;
import java.util.Map;

/**
 * @deprecated 2023.4 网站已经关闭
 * 页面的2000页是假的、其实不到400页、有时访问不通
 * http://www.nimadaili.com/gaoni/1
 * http://www.nimadaili.com/http/1
 * http://www.nimadaili.com/https/1
 *
 * <tr>
 * <th>代理IP</th>
 * <th>代理协议</th>
 * <th>匿名度</th>
 * <th>代理位置</th>
 * <th>响应速度</th>
 * <th>存活时间</th>
 * <th>打分</th>
 * </tr>
 * <tr>
 * <td>58.253.151.158:9999</td>
 * <td>HTTPS代理</td>
 * <td>普通代理</td>
 * <td>中国 河南 平顶山 联通</td>
 * <td>1.67</td>
 * <td>15小时 35分钟 15秒</td>
 * <td>350</td>
 * </tr>
 *
 * @author lym
 */
//@Component
public class NiMaDailiResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public NiMaDailiResultHandler() {
        super("xsDaili");
    }

    protected Duration defaultLimitRateDuration() {
        return Duration.ofSeconds(2);
    }

    @Override
    public void startSpiders() {
        IpProxySpiderTask task = createTask("http://www.nimadaili.com/gaoni/1");
        SpiderTaskQueue.putTask(task);

        task = createTask("http://www.nimadaili.com/http/1");
        SpiderTaskQueue.putTask(task);

        task = createTask("http://www.nimadaili.com/https/1");
        SpiderTaskQueue.putTask(task);
    }


    /**
     * 每 5 分钟触发一次
     */
    @Override
    public String executeCron() {
        return "0 0/5 * * * ?";
    }

    /**
     * 处理响应
     */
    @Override
    public void doHandle(IpProxySpiderTask task, String html) {

        String regex = "<tr>" +
                "\\s*?<td>(.*?):(\\d+)</td>" +
                "\\s*?<td>(.*?)</td>" +
                "\\s*?<td>(.*?)</td>" +
                "\\s*?<td>(.*?)</td>" +
                "\\s*?<td>(.*?)</td>" +
                "\\s*?<td>.*?</td>" +
                "\\s*?<td>(.*?)</td>" +
                "\\s*?</tr>";

        List<String> ipList = ReUtil.findAll(regex, html, 1);
        List<String> portList = ReUtil.findAll(regex, html, 2);
        List<String> typeList = ReUtil.findAll(regex, html, 3);
        List<String> anonymityList = ReUtil.findAll(regex, html, 4);
        List<String> locationAndOperatorList = ReUtil.findAll(regex, html, 5);
        List<String> responseTimeList = ReUtil.findAll(regex, html, 6);
        List<String> scoreList = ReUtil.findAll(regex, html, 7);

        for (int i = 0; i < ipList.size(); i++) {
            int splitIndex = locationAndOperatorList.get(i).lastIndexOf(' ');
            String location = locationAndOperatorList.get(i).substring(0, splitIndex);
            String operator = locationAndOperatorList.get(i).substring(splitIndex + 1);
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source(taskName)
                    .ip(ipList.get(i))
                    .port(Integer.parseInt(portList.get(i)))
//                    .type(ProxyInfo.resolveType(typeList.get(i)))
//                    .supportHttps(typeList.get(i).contains("HTTPS"))
                    .anonymity(anonymityList.get(i).contains("高匿") ? "高匿" : "普通")
                    .location(location)
                    .operator(operator)
                    .responseTime(ProxyInfo.parseSecondToMill(responseTimeList.get(i)))
                    .extendInfo(Map.of("score", scoreList.get(i)))
                    .build();
            // 保存到文件
            addProxyToSourceRepository(proxyInfo);
        }

        if (ipList.size() > 0) {
            // 下一页
            String currentPageNum = ReUtil.findAllGroup1("(\\d+)", task.getUrl()).get(0);
            int nextPageNum = Integer.parseInt(currentPageNum) + 1;
            IpProxySpiderTask nextPageTask = createTask(task.getUrl().replace(currentPageNum, nextPageNum + ""));
            SpiderTaskQueue.putTask(nextPageTask);
        }
    }

}